<ol class="breadcrumb">
    <li> <i class="fa fa-anchor"></i>
        &nbsp;地区公司管理
    </li>
    <li>公司配置</li>
</ol>

<div class="col-xs-12 info-center">
    <div class="bs-callout bs-callout-warning">
        <h4>
            系统使用单位: <strong>{:C('COLLEGE_NAME')}</strong>
        </h4>

        <form id="add_school" action="__URL__/addCompany" method="post">

            <div class="col-xs-5" style=" width:530px;">
                <div class="input-group">
                    <span class="input-group-addon" style="width:80px;background-color: #ffffff;">所在地</span>
                    <select id="location-f" name="location_f_1" class="form-control" style="width:139px;">
                        <option value="0">--</option>
                        <volist name="china" id="vo">
                            <option value="{$vo.code}">{$vo.name}</option>
                        </volist>
                    </select>

                    <select id="location-s" name="location_s_1" class="form-control" style="width:125px;border-left:0">
                        <option value="0">--</option>
                        <volist name="china" id="vo">
                            <volist name="vo['area']" id="vovo">
                                <option value="{$vovo.code}" class="{$vo.code}" >{$vovo.name}</option>
                            </volist>
                        </volist>
                    </select>

                    <select id="location-t" name="location_t_1" class="form-control required" style="width:125px;border-left:0">
                        <option value="0">--</option>
                        <volist name="china" id="vo">
                            <volist name="vo['area']" id="vovo">
                                <volist name="vovo['area']" id="vovovo">
                                    <option value="{$vovovo.code}" class="{$vovo.code}" >{$vovovo.name}</option>
                                </volist>
                            </volist>
                        </volist>
                    </select></div>
            </div>

            <div class="col-xs-4 search-col">
                <div class="input-group">
                    <input type="text" class="form-control" name="school" id="school">
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="submit"><i class="fa fa-plus"></i>&nbsp;新增公司</button>
                    </span>
                </div>
            </div>
        </form>
        <div class="clearfix"></div>
    </div>
</div>

<div class="col-xs-12 info-center">

    <div class="panel panel-mcit">

        <div class="panel-heading"> <i class="fa fa-list"></i>
            &nbsp;&nbsp;公司及部门设置
        </div>
        <table class="table table-bordered ">

            <tbody id="DataTableContent">
                <tr id="school_list">
                    <th>公司名称</th>
                    <th width="15%">所在地</th>
                    <th width="10%">部门设置</th>
                    <th width="10%">修改</th>
                    <th width="10%">删除</th>
                </tr>
                <empty name="school">
                    <td colspan="99" style="text-align:center" class="danger"> <strong><i class="fa fa-meh-o"></i>
                            &nbsp;&nbsp;请添加公司及部门数据</strong>
                    </td>
                    <else />
                    <volist name="school" id="data">
                        <if condition="$data.id eq $newid">
                            <tr class="danger tr-school">
                        <else/>
                            <tr class="tr-school">
                        </if>
                            <td>
                                <p class="school_text">{$data.name}</p>
                                <div class="col-xs-6" style="display:none">
                                    <div class="input-group">
                                        <input type="text" class="form-control input-sm school_input" value="{$data.name}" name="school" >
                                        <span class="input-group-btn">
                                            <a class="btn btn-default btn-sm edit_school" data-old="{$data.name}" data-id="{$data.id}"><i class="fa fa-refresh"></i>&nbsp;保存</a>
                                        </span>
                                    </div>
                                </div>
                            </td>
                            <td><span style="color: #000000;" title="{$data.location}">{$data.location_text}</span></td>
                            <td>
                                <a class="btn btn-sm btn-warning major"><i class="fa fa-cogs"></i>&nbsp;部门设置</a>
                            </td>
                            <td>
                                <a class="btn btn-sm btn-warning editSchool"><i class="fa fa-edit"></i>&nbsp;修改</a>
                            </td>
                            <td>
                                <a class="btn btn-sm btn-danger del_school" data-id="{$data.id}"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
                            </td>
                        </tr>
                        <if condition="$data.id eq $newid">
                            <tr class="mcit_datail">
                        <else/>
                            <tr class="mcit_datail" style="display:none">
                        </if>

                            <td colspan="4" class="bs-example-bg-classes">
                                <div class="col-xs-6">
                                    <div class="input-group">
                                        <input type="text" class="form-control major_input" name="major">
                                        <span class="input-group-btn">
                                            <a class="btn btn-default add_major"><i class="fa fa-plus"></i>&nbsp;新增部门</a>
                                        </span>
                                        <input type="hidden" name="school" value="{$data.id}"></div>
                                </div>
                                <div class="clearfix" ></div>
                                <volist name="data['major']" id="vo">
                                    <div class="col-xs-6">
                                        <p class="bg-info">
                                            <strong>{$vo.name}</strong>
                                            <a class="btn btn-link btn-xs pull-right del_major" data-id="{$vo.id}"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
                                        </p>
                                    </div>
                                </volist>
                            </td>
                        </tr>
                    </volist>
                </empty>

            </tbody>
        </table>
        <input type="hidden" name="fsign" id="fsign" value="normal"></div>
</div>
<import file="js.jquery#chained" />
<import type='css' file="css.jquery#autocomplete" />
<import file="js.jquery#autocomplete#min" />
<script type="text/javascript">
var addMajorObj;
var removeMajorObj;
var editSchoolObj;
var removeSchoolObj;
$(document).ready(function () {
    $("#location-s").chained("#location-f");
    $("#location-t").chained("#location-s");
    $("#add_school").ajaxForm({
        dataType:'json',
        clearForm:true,
        beforeSubmit:function(){
            if($("#school").val()==''){
                alertify.error("请填写要添加的公司名称");
                return false;
            }
            return true
        },
        success:function(data){
            if(data.state==100){
                alertify.success(data.msg);
                location.href = data.url;
            }else{
                alertify.error(data.msg);
            }
        }
    });
    $("#DataTableContent").on("click",'.major',function(){
        $(this).parents('tr').next().fadeToggle();
    });
    $("#DataTableContent").on("click",'.del_school',function(){
        var id = $(this).attr('data-id');
        removeSchoolObj = $(this).parents('tr');
        alertify.confirm("确定要删除此公司？", function (e) {
            if (e) {
                $.ajax({
                    type: "POST",
                    url:  '{:U("Company/delSchool")}',
                    data: {id:id},
                    dataType: "json",
                    success: function(data){
                        if(data.state==100){
                            alertify.success(data.msg);
                            removeSchoolObj.next().remove();
                            removeSchoolObj.remove();
                        }else{
                            alertify.error(data.msg);
                        }
                    }
                });
            } else {
                alertify.error("取消操作！");
            }
        }, "mcit_default");

    });

    $("#DataTableContent").on("click",'.del_major',function(){
        var id = $(this).attr('data-id');
        removeMajorObj = $(this).parents('.col-xs-6');

        alertify.confirm("确定要删除此部门？", function (e) {
            if (e) {
                //alertify.success(id);
                $.ajax({
                    type: "POST",
                    url:  '{:U("Company/delMajor")}',
                    data: {id:id},
                    dataType: "json",
                    success: function(data){
                        if(data.state==100){
                            alertify.success(data.msg);
                            removeMajorObj.remove();
                        }else{
                            alertify.error(data.msg);
                        }
                    }
                });
            } else {
                alertify.error("取消操作！");
            }
        }, "mcit_default");


    });
    $("#DataTableContent").on("click",'.add_major',function(){
        var major  = $(this).parent().prev().val();
        $(this).parent().prev().val('');
        var school = $(this).parent().next().val();
        addMajorObj = $(this).parents('.col-xs-6').next();
        if( major=='' ){
            alertify.error("请填写要添加的部门");
        }else{
            $.ajax({
                type: "POST",
                url:  '{:U("Company/addMajor")}',
                data: {major:major,school:school},
                dataType: "json",
                success: function(data){
                    if(data.state==100){
                        alertify.success(data.msg);
                        var addStr = '<div class="col-xs-6"><p class="bg-info"><strong>'+data.name+'</strong><a class="btn btn-link btn-xs pull-right del_major" data-id="'+data.id+'"><i class="fa fa-trash-o"></i>&nbsp;删除</a></p></div>';
                        addMajorObj.after(addStr);
                    }else{
                        alertify.error(data.msg);
                    }
                }
            });
        }

    });

    $("#DataTableContent").on("click",'.editSchool',function(){
        var obj  = $(this).parent().prev().prev().prev();
        obj.find('.school_text').hide();
        obj.find('.col-xs-6').show();
    });

    $("#DataTableContent").on("click",'.edit_school',function(){
        editSchoolObj = $(this).parents('.col-xs-6');
        var school    = $(this).parent().prev().val();
        var id        = $(this).attr('data-id');
        var old       = $(this).attr('data-old');
        // alertify.success(school);
        // alertify.success(id);
        // alertify.success(old);
        if(old == school){
            alertify.success("保存成功");
            editSchoolObj.hide();
            editSchoolObj.prev().show();
        }else{
            $.ajax({
                type: "POST",
                url:  '{:U("Company/editSchool")}',
                data: {id:id,school:school},
                dataType: "json",
                success: function(data){
                    if(data.state==100){
                        alertify.success(data.msg);
                        editSchoolObj.hide();
                        editSchoolObj.find('.edit_school').attr('data-old',data.school);
                        editSchoolObj.prev().html(data.school);
                        editSchoolObj.prev().show();
                    }else{
                        alertify.error(data.msg);
                    }
                }
            });
        }
    });

    $('.major_input').AutoComplete({
        'data': "__URL__/major",
        'ajaxDataType': 'json',
        'width': 'auto',
        'maxItems': 10,
        'onerror': function(msg){alert(msg);}
    });

});
</script>